<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台登录页</title>
    <script src="lib/constant.js"></script>
    <script src="lib/jquery-3.4.1.js"></script>
    <script src="lib/ajax.js"></script>
    <link rel="stylesheet" href="lib/layui/css/layui.css">
    <style>
        .main{
            margin:auto;
            border:1px solid #01AAED;
            border-radius: 20px;
            padding: 20px;
            height: 200px;
            width: 300px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
        }
    </style>
</head>
<body>
<div class="main">
<span class="layui-form-label" style="width: 200px;text-align: center;"><h3 style="font-weight: 700;">管理员登录页面</h3></span>
<div class="layui-form-item">
    <label class="layui-form-label" >管理员账号</label>
    <div class="layui-input-block">
        <input type="text" name="username"  id="username" maxlength="16" required  lay-verify="required" placeholder="请输入账户名" autocomplete="off" class="layui-input" style="width:160px;" />
    </div>
</div>
<div class="layui-form-item">
    <label class="layui-form-label">管理员密码</label>
    <div class="layui-input-block">
        <input type="password" name="password"  id="password" maxlength="16" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input" style="width:160px;" />
    </div>
</div>

    <input type="submit" value="登录" id="submit" class="layui-btn" onclick="adminLogin()" style="padding-left: 30px;padding-right: 30px;"/>
</div>
</body>
<script>

  function adminLogin(){
      let name = $("#username").val().trim();
      let pwd = $("#password").val().trim();
      if(check(username,password)){
          var obj = JSON.stringify({
              "username":name,
              "password":pwd
          })
          $.ajax({
              async: true,
              type:'post',
              url: "/admin/login",
              data: obj,
              contentType:"application/json",
              success: function(result) {
                  if(result.code==200){
                      // addCookie("id",result.data.id,7200);
                      // addCookie("username",result.data.username,7200)
                      //获取token
                      // var req = new XMLHttpRequest();
                      // req.open('GET', document.location, false);
                      // req.send(null);
                      // let token = req.getResponseHeader("Authorization");
                      // setCookie("token",token,7200);

                      window.location.href="admin/home.html";
                  }
                  else if(result.code==400){
                      alert("账号或密码错误");
                  }
              },
              complete(xhr,data){
                  var token = xhr.getResponseHeader("Authorization");
                  addCookie("token",token,7200);
              }
          });
      }
  }



  function check(username,password){
      if(username==''||username==null||password==''||password==null) {
          alert("请输入用户名/密码");
          return false;
      }
      return true;
  }
</script>
</html>